"""
Case Type   : DBlink功能
Case Name   : 创建dblink权限校验
Create At   : 2024/4/18
Owner       : peilinqian
Description :
    step1: 清理已存在用户并创建用户;
    step2: 清理已存在用户并创建数据库;
    step3: 普通用户进行dblink创建;
    step4: 系统用户进行dblink创建;
Expect      :
    step1: 清理已存在用户并创建用户; expect:成功
    step2: 清理已存在用户并创建数据库; expect:成功
    step3: 普通用户进行dblink创建; expect:创建失败
    step4: 系统用户进行dblink创建; expect:创建成功
History     :
"""
import os
import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import Node
from yat.test import macro


class DblinkCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)}:初始化----')
        self.pri_dbuser = Node(node='PrimaryDbUser')
        self.pri_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.db_name = 'db_dblink_0001'
        self.u_sys = 'u_dblink_sys0001'
        self.u_com = 'u_dblink_com0001'
        self.err_msg = 'ERROR:  permission denied to create extension .*\n' \
                       '*HINT:  Must be system admin to create this extension.*'
        self.connect_sys = f'-U {self.u_sys} -W {macro.PASSWD_INITIAL}'
        self.connect_com = f'-U {self.u_com} -W {macro.PASSWD_INITIAL}'

    def test_main(self):
        step_txt = '----step1: 清理已存在用户并创建用户; expect:成功----'
        self.log.info(step_txt)
        sql = f"drop user if exists {self.u_sys},{self.u_com} cascade;" \
              f"create user {self.u_sys} sysadmin " \
              f"password '{macro.PASSWD_INITIAL}';" \
              f"create user {self.u_com} password '{macro.PASSWD_INITIAL}';"
        result = self.pri_sh.execut_db_sql(sql)
        self.log.info(result)
        assert_flag = result.count(self.constant.CREATE_ROLE_SUCCESS_MSG)
        self.assertEqual(assert_flag, 2, "执行失败" + step_txt)

        step_txt = '----step2: 清理已存在用户并创建数据库; expect:成功----'
        self.log.info(step_txt)
        sql = f"drop database if exists {self.db_name};" \
              f"create database {self.db_name}"
        result = self.pri_sh.execut_db_sql(sql)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, result,
                      "执行失败" + step_txt)

        step_txt = '----step3: 普通用户进行dblink创建; expect:创建失败----'
        create_sql = f"create extension dblink;"
        self.log.info(step_txt)
        result = self.pri_sh.execut_db_sql(create_sql,
                                           sql_type=self.connect_com,
                                           dbname=self.db_name)
        self.log.info(result)
        self.assertRegex(result, self.err_msg, '执行失败:' + step_txt)

        step_txt = '----step4: 系统用户进行dblink创建; expect:创建成功----'
        self.log.info(step_txt)
        result = self.pri_sh.execut_db_sql(create_sql,
                                           sql_type=self.connect_sys,
                                           dbname=self.db_name)
        self.log.info(result)
        self.assertIn(self.constant.create_extension_success, result,
                      "执行失败" + step_txt)

    def tearDown(self):
        self.log.info('----this is tearDown----')
        step_txt = '----step:清理数据; expect:清理成功----'
        self.log.info(step_txt)
        drop_sql = f"drop database {self.db_name};" \
                   f"drop user if exists {self.u_sys},{self.u_com} cascade;"
        result1 = self.pri_sh.execut_db_sql(drop_sql)
        self.log.info(result1)

        self.log.info('----teardown断言----')
        self.assertIn(self.constant.DROP_DATABASE_SUCCESS, result1,
                      "执行失败" + step_txt)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG, result1,
                      "执行失败" + step_txt)

        self.log.info(f'----{os.path.basename(__file__)}:执行完毕----')
